package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.reports.forms.QADReportsForm;
import com.scalar.chiptrack.operations.reports.dao.QADReportsDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * User: sujankumar.suppala
 * Date: Apr 5, 2006
 * Time: 10:59:54 AM
 */
public class QADReportsAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/qadReportsAction.do" ;

    public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        QADReportsForm qadReportsForm = ( QADReportsForm )form;
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);

        if (user == null)
        {
            session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward ("login"));
        }
        HashMap info = new HashMap();
        HashMap errors = new HashMap();
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        String action = request.getParameter( "action" );
        if ( action == null || action.trim().length() == 0 )
        {
            action = "woWCDetails";
        }

        ArrayList processTypeNameList   = ProductDao.getAllProcessTypeNames();
        ArrayList packageTypeList = QADReportsDAO.loadPackageTypes(null);
        ArrayList cardTypeList = QADReportsDAO.loadCardTypes(null);
        ArrayList teraByteList = QADReportsDAO.loadTeraBytes(null);
        String processTypeName          = qadReportsForm.getProcessTypeName();
        if ( processTypeName != null && "Mark and Pack".equalsIgnoreCase(processTypeName) )
        {
            processTypeName = "Mark & Pack";
        }
        ArrayList baseProductList       = new ArrayList();
        ArrayList partNumberList        = new ArrayList();
        try
        {
            if ( processTypeName == null || "0".equalsIgnoreCase( processTypeName ) || "Finished Goods".equalsIgnoreCase( processTypeName ) )
            {
                baseProductList = ProductDao.loadAllBaseProducts( null );
            }
            else //NO NEED TO DISPLAY 'BOARDS' AND 'KITS'
            {
                baseProductList = ProductDao.loadChipBaseProducts( null );
            }
            partNumberList = ProductDao.getPartNumberByBaseProductAndPsType(processTypeName,qadReportsForm.getBaseProductId());
        }
        catch ( ScalarException e )
        {
            e.printStackTrace();
            errors.put ( "Could not load search field values", null );
        }
        request.setAttribute ( "baseProductList", baseProductList );
        request.setAttribute ("processTypeNameList", processTypeNameList);
        request.setAttribute ( "partNumberList", partNumberList );
        request.setAttribute ("packageTypeList", packageTypeList);
        request.setAttribute ("cardTypeList", cardTypeList);
        request.setAttribute ("teraByteList", teraByteList);

        if ( "woWCDetails".equalsIgnoreCase( action ) )
        {
            return mapping.findForward( "woWCDetails" );
        }
        else if ( "woNotStartedPerPlan".equalsIgnoreCase( action ) )
        {
            return mapping.findForward( "woNotStartedPerPlan" );
        }
        else if ( "mismatchesOfQADVendor".equalsIgnoreCase( action ) )
        {
            return mapping.findForward( "mismatchesOfQADVendor" );
        }
        else if ( "workOrderStatus".equalsIgnoreCase( action ) )
        {
            return mapping.findForward( "workOrderStatus" );
        }
        else if ( "performance".equalsIgnoreCase( action ) )
        {
            return mapping.findForward( "performance" );
        }
        else if ( "woNotStartedPerPlanReport".equalsIgnoreCase( action ) )
        {
            LinkedHashMap workOrdersNotStarted;
            try
            {
                workOrdersNotStarted = QADReportsDAO.getWONotStartedPerPlan( null, qadReportsForm );
                qadReportsForm.setWoMap( workOrdersNotStarted );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not load Work Orders not started per plan", null );
                return mapping.findForward ( "woNotStartedPerPlan");
            }
            return mapping.findForward( "woNotStartedPerPlanReport" );
        }
        else if ( "dueDateVsVendorCommitDate".equalsIgnoreCase( action ) )
        {
            try
            {
                LinkedHashMap woDueDateVsCommitDateMap = QADReportsDAO.getWODueDateVsVendorsCommitDate(null,qadReportsForm);
                qadReportsForm.setWoMap( woDueDateVsCommitDateMap );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not get work orders for QAD Due Date Vs Vendor's Commit Date", null );
                return mapping.findForward( "mismatchesOfQADVendor" );
            }
            return mapping.findForward( "dueDateVsVendorCommitDateReport" );
        }
        else if ( "stoppedWorkOrders".equalsIgnoreCase( action ) )
        {
            try
            {
                LinkedHashMap stoppedWorkOrders = QADReportsDAO.getStoppedWorkOrders(null, qadReportsForm);
                qadReportsForm.setWoMap(stoppedWorkOrders);
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not load Stopped Work Orders", e );
                return mapping.findForward( "workOrderStatus" );
            }
            return mapping.findForward( "stoppedWorkOrdersReport" );
        }
        return null;
    }
}
